package com.juyu.miliao.api.dao.redis;


import com.juyu.miliao.api.util.RedisKeys;
import com.juyu.miliao.common.constant.enumRedisKey.EnumRedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Set;

@Repository
public class LineTimeRedisDao {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	public void addLine(String accid, String timestamp) {
		stringRedisTemplate.opsForHash().put(RedisKeys.HASH_COUNTLINE.getValue(), accid, timestamp);
	}

	public String findLine(String accid) {
		return stringRedisTemplate.opsForHash().get(RedisKeys.HASH_COUNTLINE.getValue(),accid).toString();
	}

	public void deleteLine(String accid) {
		stringRedisTemplate.opsForHash().delete(RedisKeys.HASH_COUNTLINE.getValue(), accid);
	}

	public Boolean hasKeyLine(String accid) {
		return stringRedisTemplate.opsForHash().hasKey(RedisKeys.HASH_COUNTLINE.getValue(), accid);
	}

	public Set<Object> getLineHashKeys(){
		return stringRedisTemplate.opsForHash().keys(RedisKeys.HASH_COUNTLINE.getValue());
	}
	
	//主播操作
	public void addHostLine(String accid) {
		stringRedisTemplate.opsForSet().add(RedisKeys.SET_HOSTLINE.getValue(), accid);
	}

	public void deleteHostLine(String accid) {
		stringRedisTemplate.opsForSet().remove(RedisKeys.SET_HOSTLINE.getValue(), accid);
	}

    /**
     * 判断主播是否在线
     *
     * @param accid userId
     * @return boolean
     */
    public Boolean isMember(String accid) {
        return stringRedisTemplate.opsForSet().isMember(RedisKeys.SET_HOSTLINE.getValue(), accid);
    }

	public Set<String> getHostLine() {
		return stringRedisTemplate.opsForSet().members(RedisKeys.SET_HOSTLINE.getValue());
	}

	public void addHostLineBySex(int sex ,String accid){
		stringRedisTemplate.opsForSet().add(RedisKeys.SET_HOSTLINE_SEX.getValue(sex), accid);
	}

	public void deleteHostLineBySex(int sex ,String accid){
		stringRedisTemplate.opsForSet().remove(RedisKeys.SET_HOSTLINE_SEX.getValue(sex), accid);
	}
	public Set<String> getHostLineBySex(int sex) {
		return stringRedisTemplate.opsForSet().members(RedisKeys.SET_HOSTLINE_SEX.getValue(sex));
	}

	public void addCommonUser(String accid,int vGradeSum) {
		stringRedisTemplate.opsForZSet().add(RedisKeys.ZSET_COMMONSORTONLINE.getValue(), accid, vGradeSum);
	}

	public void deleteCommonUser(String accid) {
		stringRedisTemplate.opsForZSet().remove(RedisKeys.ZSET_COMMONSORTONLINE.getValue(), accid);
	}

	public Optional<Long> isExist(String userId) {
		Long rank = stringRedisTemplate.opsForZSet().rank(RedisKeys.ZSET_COMMONSORTONLINE.getValue(), userId);
		return Optional.ofNullable(rank);
	}

	public void CommonIncrementScore(String userId,int sort) {
		stringRedisTemplate.opsForZSet().incrementScore(RedisKeys.ZSET_COMMONSORTONLINE.getValue(), userId,sort);
	}
	//添加在线用户
	public void addOnLineUserBySex(int sex, String accid ,int sort) {
		stringRedisTemplate.opsForZSet().add(RedisKeys.ZSET_USER_SEX_SORT.getValue(sex), accid,sort);
	}


	//删除在线用户
	public void deleteOnLineUserBySex(int sex ,String accid){
		stringRedisTemplate.opsForZSet().remove(RedisKeys.ZSET_USER_SEX_SORT.getValue(sex), accid);
	}

	/** 03-13粉包需求：判断用户是否是mmjy包登录*/
	public boolean isMemberUserLoginPackageId(String accid,String appPackageId){
		return stringRedisTemplate.opsForSet().isMember(EnumRedisKey.SET_USER_APPPACKAGEID.getValue(appPackageId),accid);
	}

	public void addOnLineByAppPackageIdBySex(int sex ,String accid,String appPackageId){
		stringRedisTemplate.opsForSet().add(EnumRedisKey.SET_USER_ONLINE_APPPACKAGEID_SEX.getValue(appPackageId,sex),accid);
	}

	public void removeOnLineByAppPackageIdBySex(int sex ,String accid,String appPackageId){
		stringRedisTemplate.opsForSet().remove(EnumRedisKey.SET_USER_ONLINE_APPPACKAGEID_SEX.getValue(appPackageId,sex),accid);
	}

	public boolean isMemberThreeDaysNewUser(String accid){
		Long temp = stringRedisTemplate.opsForZSet().rank("threeDaysNewUser",accid);
		if(temp == null || temp<0){
			return false;
		}
		return true;
	}

	public void addOnLineThreeDaysNewUser(String accid) {
		stringRedisTemplate.opsForZSet().add("onLineThreeDaysNewUser",accid,System.currentTimeMillis());
	}
	public void removeOnLineThreeDaysNewUser(String accid){
		stringRedisTemplate.opsForZSet().remove("onLineThreeDaysNewUser",accid);
	}

	public Boolean addOnlineRechargeAmountUser(String accid) {
		return stringRedisTemplate.opsForZSet().add("onLineRechargeAmountUser",accid,System.currentTimeMillis());
	}
	public void removeOnlineRechargeAmountUser(String accid) {
		stringRedisTemplate.opsForZSet().remove("onLineRechargeAmountUser",accid);
	}


	//新人主播使用
	public boolean isMemberThirtyDaysNewHost(String accid){
		Long temp = stringRedisTemplate.opsForZSet().rank("thirtyDaysNewHost",accid);
		if(temp == null || temp<0){
			return false;
		}
		return true;
	}

	public void addOnLineThirtyDaysNewHost(String accid) {
		stringRedisTemplate.opsForSet().add("onLineThreeDaysNewHost",accid);
	}
	public void removeOnLineThirtyDaysNewHost(String accid){
		stringRedisTemplate.opsForSet().remove("onLineThreeDaysNewHost",accid);
	}

	//1.5.6 保存主播离线时间-新人
	public void setHostUserSingOutDateTimeByNew(String accid) {
		long timestamp = Instant.now().toEpochMilli();// 精确到毫秒
		stringRedisTemplate.opsForZSet().add("hostUserSingOutDateTime:ByNew",accid,timestamp);
	}
	//1.5.6 保存主播离线时间-热门
	public void setHostUserSingOutDateTimeByHost(String accid) {
		long timestamp = Instant.now().toEpochMilli();// 精确到毫秒
		stringRedisTemplate.opsForZSet().add("hostUserSingOutDateTime:ByHost",accid,timestamp);
	}

	public void removeHostUserSingOutDateTimeByNew(String accid) {
		stringRedisTemplate.opsForZSet().remove("hostUserSingOutDateTime:ByNew",accid);
	}

	public void removeHostUserSingOutDateTimeByHost(String accid) {
		stringRedisTemplate.opsForZSet().remove("hostUserSingOutDateTime:ByHost",accid);
	}

	//1.5.6 获取主播每小时收益
	public double getHomeListIncomeByHostUserByUserId(long userId) {
		LocalDateTime dt = LocalDateTime.now();
		//homeListIncomeByHostUser:2019-10-24:13
		String time = dt.getYear()+"-"+dt.getMonthValue()+"-"+dt.getDayOfMonth()+":"+dt.getHour();
		Double score = stringRedisTemplate.opsForZSet().score("homeListIncomeByHostUser:"+time,userId+"");
		if(score==null){
			return 0;
		}
		return score;
	}

	// 获取主播离线时间
	public Double getHostUserSingOutDateTimeByHost(long userId) {
		Double score =stringRedisTemplate.opsForZSet().score("hostUserSingOutDateTime:ByHost",userId+"");
		if(score==null){
			return 0d;
		}
		return score;
	}
}
